package com.dwsoft.utils;

import com.dwsoft.xml.*;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.Xpp3Driver;

/**
 * @author sqw
 * @version 1.0
 * @description TODO
 * @ClassName XStreamUtil
 * @Date 2020/8/7
 * @since jdk1.8
 */
public class XStreamUtil {

    private static XStream xStream;

    static{
        xStream = new XStream(new FieldDefaultValueProvider(),new Xpp3Driver());
        xStream.processAnnotations(XDocument.class); //类加载 必须
        xStream.processAnnotations(XHeading1.class);
        xStream.processAnnotations(XHeading2.class);
        xStream.processAnnotations(XHeading3.class);
        xStream.processAnnotations(XParagraph.class);
        xStream.processAnnotations(XParagraphPiece.class);
        xStream.processAnnotations(XTable.class);
        xStream.processAnnotations(XBar.class);
        xStream.processAnnotations(XLine.class);
    }

    //xml转java对象
    public static Object xmlToBean(String xml){
        return xStream.fromXML(xml);
    }

    //java对象转xml
    public static String beanToXml(Object obj){
        return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + xStream.toXML(obj);
    }
}
